home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / REGISTER.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  8KB  |  333 lines

  1. /*
  2.  * REGISTER.C
  3.  *
  4.  * Functions to perform enumerations and lookups into the registration
  5.  * database for the use of OLE client applications.
  6.  *
  7.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  8.  */
  9.  
  10. #include <windows.h>
  11. #include <shellapi.h>
  12. #include "register.h"
  13.  
  14.  
  15. /*
  16.  * WFillClassList
  17.  *
  18.  * Purpose:
  19.  *  Enumerates available OLE object classes from the registration
  20.  *  database and fills a listbox with those names.
  21.  *
  22.  *  Note that this function removes any prior contents of the listbox.
  23.  *
  24.  * Parameters:
  25.  *  hList           HWND to the listbox to fill.
  26.  *
  27.  * Return Value:
  28.  *  WORD            Number of strings added to the listbox, -1 on failure.
  29.  */
  30.  
  31. WORD FAR PASCAL WFillClassList(HWND hList)
  32.     {
  33.     DWORD       dw;
  34.     WORD        cStrings;
  35.     HKEY        hKey;
  36.     LONG        lRet;
  37.     char        szExec[CBMAXKEY];
  38.     char        szClass[CBMAXKEY];
  39.     char        szKey[CBMAXKEY];
  40.  
  41.     //Clean out the existing strings.
  42.     SendMessage(hList, LB_RESETCONTENT, 0, 0L);
  43.  
  44.     //Open up the root key.
  45.     lRet=RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKey);
  46.  
  47.     if ((LONG)ERROR_SUCCESS!=lRet)
  48.         return (WORD)-1;
  49.  
  50.     cStrings=0;
  51.     lRet=RegEnumKey(hKey, cStrings++, szClass, CBMAXKEY);
  52.  
  53.     while ((LONG)ERROR_SUCCESS==lRet)
  54.         {
  55.         //Check for a \protocol\StdFileEditing\server entry.
  56.         lstrcpy(szExec, szClass);
  57.         lstrcat(szExec, "\\protocol\\StdFileEditing\\server");
  58.  
  59.         dw=CBMAXKEY;
  60.  
  61.         lRet=RegQueryValue(hKey, szExec, szKey, &dw);
  62.  
  63.         if ((LONG)ERROR_SUCCESS==lRet)
  64.             {
  65.             dw=CBMAXKEY;
  66.  
  67.             /*
  68.              * If we have an .EXE, get the class name and send it to
  69.              * the listbox.
  70.              */
  71.             lRet=RegQueryValue(hKey, szClass, szKey, &dw);
  72.  
  73.             if ((LONG)ERROR_SUCCESS==lRet)
  74.                 SendMessage(hList, LB_ADDSTRING, 0, (DWORD)(LPSTR)szKey);
  75.             }
  76.  
  77.         //Continue with the next key.
  78.         lRet=RegEnumKey(hKey, cStrings++, szClass, CBMAXKEY);
  79.         }
  80.  
  81.     RegCloseKey(hKey);
  82.     return cStrings;
  83.     }
  84.  
  85.  
  86.  
  87.  
  88. /*
  89.  * WClassFromDescription
  90.  *
  91.  * Purpose:
  92.  *  Looks up the actual OLE class name in the registration database
  93.  *  for the given descriptive name chosen from a listbox.
  94.  *
  95.  * Parameters:
  96.  *  psz             LPSTR to the descriptive name.
  97.  *  pszClass        LPSTR in which to store the class name.
  98.  *  cb              WORD maximum length of pszClass.
  99.  *
  100.  * Return Value:
  101.  *  WORD            Number of characters copied to pszClass.  0 on failure.
  102.  */
  103.  
  104. WORD FAR PASCAL WClassFromDescription(LPSTR psz, LPSTR pszClass, WORD cb)
  105.     {
  106.     DWORD           dw;
  107.     HKEY            hKey;
  108.     char            szClass[CBMAXKEY];
  109.     LONG            lRet;
  110.     WORD            i;
  111.  
  112.     //Open up the root key.
  113.     lRet=RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKey);
  114.  
  115.     if ((LONG)ERROR_SUCCESS!=lRet)
  116.         return 0;
  117.  
  118.     i=0;
  119.     lRet=RegEnumKey(hKey, i++, szClass, CBMAXKEY);
  120.  
  121.     while ((LONG)ERROR_SUCCESS==lRet)
  122.         {
  123.         dw=(DWORD)cb;
  124.         lRet=RegQueryValue(hKey, szClass, pszClass, &dw);
  125.  
  126.         if ((LONG)ERROR_SUCCESS==lRet)
  127.             {
  128.             if (!lstrcmp(pszClass, psz))
  129.                 break;
  130.             }
  131.  
  132.         //Continue with the next key.
  133.         lRet=RegEnumKey(hKey, i++, szClass, CBMAXKEY);
  134.         }
  135.  
  136.     if ((LONG)ERROR_SUCCESS==lRet)
  137.         lstrcpy(pszClass, szClass);
  138.     else
  139.         dw=0L;
  140.  
  141.     RegCloseKey(hKey);
  142.     return (WORD)dw;
  143.     }
  144.  
  145.  
  146.  
  147. /*
  148.  * WClassFromExtension
  149.  *
  150.  * Purpose:
  151.  *  Looks up the OLE class name name in the registration database for
  152.  *  the given file extension.
  153.  *
  154.  * Parameters:
  155.  *  pszExt          LPSTR to the extension name.
  156.  *  psz             LPSTR in which to store the class name.
  157.  *  cb              WORD maximum length of the class name.
  158.  *
  159.  * Return Value:
  160.  *  WORD            Number of characters copied to pszClass.  0 on failure.
  161.  */
  162.  
  163. WORD FAR PASCAL WClassFromExtension(LPSTR pszExt, LPSTR psz, WORD cb)
  164.     {
  165.     DWORD           dw;
  166.     HKEY            hKey;
  167.     LONG            lRet;
  168.  
  169.     if (NULL==pszExt || NULL==psz)
  170.         return 0;
  171.  
  172.     //Open up the root key.
  173.     lRet=RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKey);
  174.  
  175.     if ((LONG)ERROR_SUCCESS!=lRet)
  176.         return 0;
  177.  
  178.     //Get the class name using the extension.
  179.     dw=(DWORD)cb;
  180.     lRet=RegQueryValue(hKey, pszExt, psz, &dw);
  181.  
  182.     RegCloseKey(hKey);
  183.  
  184.     if ((LONG)ERROR_SUCCESS!=lRet)
  185.         return 0;
  186.  
  187.     return (WORD)dw;
  188.     }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. /*
  199.  * CVerbEnum
  200.  *
  201.  * Purpose:
  202.  *  Builds a double-null terminated list of verbs for a particular class.
  203.  *
  204.  * Parameters:
  205.  *  pszClass        LPSTR to the object classname.
  206.  *  pszVerbs        LPSTR in which to store the verbs.
  207.  *  cbMax           WORD maximum number of characters in pszVerbs.
  208.  *
  209.  * Return Value:
  210.  *  WORD            Number of verbs stored.
  211.  */
  212.  
  213. WORD FAR PASCAL CVerbEnum(LPSTR pszClass, LPSTR pszVerbs, WORD cbMax)
  214.     {
  215.     DWORD           dw;
  216.     WORD            cb;
  217.     HKEY            hKey;
  218.     HKEY            hKeyVerb;
  219.     LONG            lRet;
  220.     char            szVerbNum[10];
  221.     HANDLE          hMem=NULL;
  222.     WORD            i;
  223.  
  224.     if (NULL==pszClass || NULL==pszVerbs || 0==cbMax)
  225.         return NULL;
  226.  
  227.     /*
  228.      * Open up the key for <pszClass>\\protocol\\StdFileEditing\\verb
  229.      * We first open up the pszClass key from which we can open a subkey
  230.      * of the \\protocol\\StdFileEditing\\verb, which saves us from
  231.      * having to prepend that mess to each verb number.
  232.      */
  233.     lRet=RegOpenKey(HKEY_CLASSES_ROOT, pszClass, &hKey);
  234.  
  235.     if ((LONG)ERROR_SUCCESS!=lRet)
  236.         return 0;
  237.  
  238.     lRet=RegOpenKey(hKey, "protocol\\StdFileEditing\\verb", &hKeyVerb);
  239.  
  240.     //We will not need this key any more.
  241.     RegCloseKey(hKey);
  242.  
  243.     if ((LONG)ERROR_SUCCESS!=lRet)
  244.         return 0;
  245.  
  246.  
  247.     //Start at verb 0
  248.     i=0;
  249.     cb=0;
  250.  
  251.     /*
  252.      * Extract verbs from the registration database directly into
  253.      * the list.  cbMax is decremented each time by the number of
  254.      * character stored in the memory.
  255.      */
  256.  
  257.     while ((LONG)ERROR_SUCCESS==lRet && cbMax > 0)
  258.         {
  259.         /*
  260.          * These operations make up for the previous RegQueryValue call.
  261.          * cb includes the null terminator.
  262.          */
  263.         pszVerbs+=(DWORD)cb;
  264.         cbMax-=cb;
  265.         cb=cbMax;
  266.  
  267.         wsprintf(szVerbNum, "%d", i++);
  268.         lRet=RegQueryValue(hKeyVerb, szVerbNum, pszVerbs, &dw);
  269.         cb=(WORD)dw;
  270.         }
  271.  
  272.     //Null-terminate the list.
  273.     *pszVerbs=0;
  274.  
  275.     RegCloseKey(hKeyVerb);
  276.  
  277.     /*
  278.      * Return the number of verbs we added.  i will be one greater since
  279.      * it was incremented the last time we failed RegQueryValue.
  280.      */
  281.  
  282.     return --i;
  283.     }
  284.  
  285.  
  286.  
  287.  
  288. /*
  289.  * WDescriptionFromClass
  290.  *
  291.  * Purpose:
  292.  *  Looks up the actual OLE descriptive name name in the registration
  293.  *  database for the given class name.
  294.  *
  295.  * Parameters:
  296.  *  pszClass        LPSTR to the class name.
  297.  *  psz             LPSTR in which to store the descriptive name.
  298.  *  cb              WORD maximum length of psz.
  299.  *
  300.  * Return Value:
  301.  *  WORD            Number of characters copied to pszClass.  0 on failure.
  302.  */
  303.  
  304. WORD FAR PASCAL WDescriptionFromClass(LPSTR pszClass, LPSTR psz, WORD cb)
  305.     {
  306.     DWORD           dw;
  307.     HKEY            hKey;
  308.     LONG            lRet;
  309.  
  310.     if (NULL==pszClass || NULL==psz)
  311.         return 0;
  312.  
  313.     //Open up the root key.
  314.     lRet=RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKey);
  315.  
  316.     if ((LONG)ERROR_SUCCESS!=lRet)
  317.         return 0;
  318.  
  319.     //Get the descriptive name using the class name.
  320.     dw=(DWORD)cb;
  321.     lRet=RegQueryValue(hKey, pszClass, psz, &dw);
  322.  
  323.     RegCloseKey(hKey);
  324.  
  325.     psz+=lstrlen(psz)+1;
  326.     *psz=0;
  327.  
  328.     if ((LONG)ERROR_SUCCESS!=lRet)
  329.         return 0;
  330.  
  331.     return (WORD)dw;
  332.     }
  333.